﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ObjectBuilder2;

namespace SharedGenomics.Workbench.Core
{
    public class EventBrokerRegistrationContainerExtension : UnityContainerExtension
    {
        protected override void Initialize()
        {
            this.Context.Locator.Add(typeof(IEventBroker), this.Container.Resolve<IEventBroker>());
            this.Context.Strategies.AddNew<EventBrokerRegistrationStrategy>(Microsoft.Practices.Unity.ObjectBuilder.UnityBuildStage.Creation);
        }
    }

    public class EventBrokerRegistrationStrategy : BuilderStrategy
    {
        public override void PreBuildUp(IBuilderContext context)
        {
            if (context.Existing != null)
            {
                var eventBroker = context.Locator.Get<IEventBroker>();
                eventBroker.SubscribeAll(context.Existing);
            }
        }
    }
}
